
//#define TEST_LdapClient
#ifdef TEST_LdapClient

#include "Win32.Net.LdapClient.h"

// -----------------------------------------------------------------------------

void GetUserInput(TCHAR *prompt, TCHAR *value, DWORD valueLength)
{
   *value = NULL;
   _tprintf_s(L"%s",prompt);
   _tscanf_s(L"%s",value,valueLength);
}  // end of GetUserInput

const DWORD maxLen = Kif::Win32::String::MaxStringLength;

void main()
{
   Kif::Win32::Net::Ldap::LdapClient myLdap;

   TCHAR host[maxLen+1] ;
   TCHAR user[maxLen+1] ;
   TCHAR passwd[maxLen+1] ;

   GetUserInput(TEXT("Enter LDAP host: "),host,maxLen);
   GetUserInput(TEXT("Enter LDAP user: "),user,maxLen);
   GetUserInput(TEXT("Enter user password: "),passwd,maxLen);

   myLdap.SetLogPath(L"d:/temp/Ldap");

   _tprintf_s(L"Connecting to %s as %s (%s).\n",host,user,passwd);

   if (myLdap.Connect(host))
   {
      Kif::Win32::String nameContext;
      myLdap.GetDefaultNamingContext(nameContext) ;
      _tprintf_s(L"Naming Context is (%s).\n",nameContext.GetBuffer());
      myLdap.SetDefaultNamingContext() ;

      TCHAR usertmpl[maxLen+1] ;
      _tcscpy_s(usertmpl,maxLen,TEXT("CN=%s,CN=Users,")) ;
      _tcscat_s(usertmpl,maxLen,nameContext.GetBuffer()) ;

      myLdap.SetUserDistinguisedNameTemplate(usertmpl) ;

      _tprintf_s(L"User Distinguised Name Template is (%s).\n",usertmpl);
      _tprintf_s(L"CONNECTED TO LDAP SERVER.\n");
      _tprintf_s(L"AUTHENTICATING ...\n");

      if (myLdap.AuthenticateUser(user,passwd,
         Kif::Win32::Net::Ldap::AUTH_CLEAR_TEXT))
         _tprintf_s(L"   USER IS OK.\n");
      else
      {
         _tprintf_s(L"   USER AUTHENTICATION FAILED.\n");
      }

      myLdap.Disconnect() ;
      _tprintf_s(L"DISCONNECTED.\n");
   }
   else
   if (myLdap.Connect())
   {
      if (myLdap.AuthenticateUser())
         _tprintf_s(L"CONNECTED AS DEFAULT USER.\n");
   }
}

#endif

